package com.hc.TestDrill;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.hc.activity.MainActivity;
import com.hc.commonData.SysCommonData;
import com.hc.model.EquipmentBean;
import com.hc.model.StepKey;
import com.hc.stepKeydbservice.KeyManager;
import com.hc.util.OrderUtil;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.InputType;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * 测试训练模式业务类
 * @author tiansl
 *
 */
public class TestDrillService {

	/**
	 * 
	 * 设置训练模式的行走距离
	 * @param content
	 */
	public static  void setWlkLength( final Context context){
		

		final EditText str = new EditText(context);
		str.setInputType(InputType.TYPE_CLASS_NUMBER);
		new AlertDialog.Builder(context).setTitle("输入将要步行距离")
		.setView(str)
		.setPositiveButton("开始训练", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if ( !"".equals(str.getText().toString()) || str.getText().toString()!=null)
				{
					double stepNumber = Double.parseDouble(str.getText().toString()) ;
					
					if ( stepNumber <= 0f )
					{
						
						Toast.makeText(context.getApplicationContext(), "提示：设置失败，请输入大于0的有效数字", 0).show();
						
					}else{
						
						SysCommonData.drillWalkLength = stepNumber;
						
						if ( starTestDrill(context) )
						{
							Toast.makeText(context.getApplicationContext(), "提示：设置行走距离成功", 0).show();
							Intent  intent = new Intent (context,TestDrillActivity.class);//开启训练
							context.startActivity(intent);
							
						} else {
							
							
							Toast.makeText(context.getApplicationContext(), "提示：开启训练失败", 0).show();
							
						};
						
					}
				}
			}
		}).setNegativeButton("取消", null).create().show();
		
	
	}
	
	/**
	 * 
	 * 结束步测训练
	 * @param context
	 */
	public static void closeTestDrill( final Context context){
		
		OrderUtil.ECCONTROLTEST_order("6");//下发结束训练
		
		
		
		int i = 0;
		while ( i < 5 ) 
		{
			
			String result = OrderUtil.getEcMcuRealStatus();
		  if (result!=null&& result.equals("0") ) 
		  {
			  
			  Toast.makeText(context.getApplicationContext(), "提示：结束训练成功，正在获取训练key值。。。", 0).show();
			return ;
			
		  }
		  
		  try {
			Thread.sleep(20);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		  
		  i++;
		}
	}
	/**
	 * 
	 * 开始步测训练
	 * @param context
	 */
	public static boolean  starTestDrill( final Context context){
		
		
		OrderUtil.ECCONTROLTEST_order("5");//下发开始训练
		

		int i = 0;
		while ( i < 5 ) 
		{
			
			String result = OrderUtil.getEcMcuRealStatus();
		  if (result!=null&& result.equals("8") ) //判断是否为训练状态
		  {
			return  true;
			
		  }
		  try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		  i++;
		}
		
		return false;
	}
	/**
	 * 获取步测的key值
	 * @param context
	 */
	public static String getStepKey(Context context){
		
		
		
		int i = 0;
		while ( i < 5 ) 
		{
			
			String key =  OrderUtil.getEcStepKey();
		  if (key!=null) //判断是否为训练状态
		  {
			return key ;
			
		  }
		  try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		  i++;
		}
		
		return null;
	}
	
	/**
	 * 保存训练产生的key值
	 * @param context
	 */
	public static void saveSetpKey(final Context context,final String key){
		

		final EditText str = new EditText(context);
		new AlertDialog.Builder(context).setTitle("请输入保存标示符")
		.setView(str)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			
				if ( !"".equals(str.getText().toString()) || str.getText().toString()!=null)
				{
					SysCommonData.stepKey = (float) (SysCommonData.drillWalkLength/(Float.parseFloat(key)));
					
					
					String 	pattern = "yyyy-MM-dd HH:mm:ss";
					SimpleDateFormat formate  = new SimpleDateFormat(pattern);
					Date date =  new Date();
					KeyManager manager = new KeyManager(context);
					manager.addTestKey(new StepKey(System.currentTimeMillis()+"",str.getText().toString(),key,formate.format(date),SysCommonData.drillWalkLength+""));
					
					SysCommonData.stepKey = (float) (SysCommonData.drillWalkLength/(Float.parseFloat(key)));
					
					Intent intent = new Intent(context, MainActivity.class);
					context.startActivity(intent);
				}
			}
		}).setNegativeButton("取消", null).create().show();
		
	
	}
}
